package com.example.project.service;

import com.example.project.dao.MenuInterface;
import com.example.project.dao.RolesInterface;
import com.example.project.dao.UserInterface;
import com.example.project.entity.Menu;
import com.example.project.entity.ResponseInfo;
import com.example.project.entity.Roles;
import com.example.project.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuService {
    @Resource
    MenuInterface menuInterface;
    @Resource
    UserInterface userInterface;
    @Resource
    RolesInterface rolesInterface;
    public ResponseInfo getmenutable(Menu menu, String token) {
//        List<Menu> list = menuInterface.queryTable(menu);
        User tokenUser = userInterface.queryUser(token);
        List<Roles> tokenRoles = rolesInterface.queryRoles(tokenUser);
        String roles ="(";
        for (Roles tokenRole : tokenRoles) {
            roles+="'"+tokenRole.getId()+"',";
        }
        roles= roles.substring(0,roles.length()-1);
        roles+=")";

        List<Menu> menusList = menuInterface.queryMenuTable(roles);
        Map<String, Object> map = new HashMap<>();
//        map.put("rowslist", list);
        map.put("rows", menusList);

        return ResponseInfo.success(map);
    }

    public ResponseInfo menutable(Menu menu) {
        List<Menu> menusList = menuInterface.menutable(menu);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", menusList);

        return ResponseInfo.success(map);
    }

    public ResponseInfo addtable(Menu menu){
        if(StringUtils.hasLength(menu.getText()) &&StringUtils.hasLength(menu.getUrl() )&&
                StringUtils.hasLength(menu.getIcon()) &&StringUtils.hasLength(menu.getPid()))
        {
            menuInterface.addtable(menu);
            return ResponseInfo.success(null);}
        else {
        return ResponseInfo.fail(300,"填写为空");
    }}

    public ResponseInfo edittable(Menu menu) {
        if(StringUtils.hasLength(menu.getText()) &&StringUtils.hasLength(menu.getUrl() )&&
                StringUtils.hasLength(menu.getIcon()) &&StringUtils.hasLength(menu.getPid()))
        {
            menuInterface.edittable(menu);
            return ResponseInfo.success(null);}
        else {
            return ResponseInfo.fail(300,"填写为空");
        }}

    public ResponseInfo deltable(Menu menu) {
        long count=menuInterface.counttablepid(menu);
        if (count>0){
            return ResponseInfo.fail(300,"有下级菜单");

        }else {
            menuInterface.deltable(menu);
            return ResponseInfo.success(null);
        }

    }

    public ResponseInfo getrolemenutable(Roles roles) {
        List<Menu> rolemenusList = menuInterface.getrolemenutable(roles);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", rolemenusList);
        System.out.println("getrolemenutable: " + rolemenusList);
        return ResponseInfo.success(map);
    }
}

